<template>
    <div class="container">
        <!-- 顶部栏 -->
        <div class="header">
            <van-icon @click="goBack()" class="header-left" name="arrow-left" size="30" />
            填写服务订单
        </div>
        <!-- 步骤条 -->
        <statusBar item="0"></statusBar>
        <!-- 表单 -->
        <van-cell class="cell">
            <template #title>
                <van-image width="25" height="25" :src="createInfo.service.serviceImg">
                </van-image>
                <span class="server-name">{{ createInfo.service.serviceName }}</span>
            </template>
            <template #default>
                <div class="service-text">服务内容</div>
            </template>
        </van-cell>
        <van-cell-group class="cell">
            <van-cell>
                <template #title>
                    就诊医院
                </template>
                <template #default>
                    <div @click="showHospital = true">
                        {{ form.hospital_name || "请选择就诊医院" }}
                        <van-icon name="arrow" />
                    </div>
                </template>
            </van-cell>
            <van-cell>
                <template #title>
                    就诊时间
                </template>
                <template #default>
                    <div @click="showStartTime = true">
                        {{ currentDate || "请选择就诊时间" }}
                        <van-icon name="arrow" />
                    </div>
                </template>
            </van-cell>
            <van-cell>
                <template #title>
                    陪诊师
                </template>
                <template #default>
                    <div @click="showCompanion = true">
                        {{ companionName || "请选择陪诊师" }}
                        <van-icon name="arrow" />
                    </div>
                </template>
            </van-cell>
            <van-cell>
                <template #title>
                    接送地址
                </template>
                <template #default>
                    <van-field class="text" input-align="right" v-model="form.receiveAdress" placeholder="用填写接送地址" />
                </template>
            </van-cell>
            <van-cell>
                <template #title>
                    联系电话
                </template>
                <template #default>
                    <van-field class="text" input-align="right" v-model="form.tel" placeholder="用填写联系电话" />
                </template>
            </van-cell>
        </van-cell-group>
        <!-- 服务需求 -->
        <van-cell-group title="服务需求" class="cell">
            <van-field class="text" style="height: 100px;" v-model="form.demand" placeholder="请简单描述一下您要就诊的科室..." />
        </van-cell-group>
        <!-- 提交按钮 -->
        <van-button @click="submit()" class="sumbit" type="primary" size="large">提交订单</van-button>
        <!-- 弹出层 -->
        <van-popup v-model:show="showHospital" position="bottom" :style="{ height: '30%' }">
            <van-picker :columns="showHospitalColumns" @confirm="showHospConfirm" @cancel="showHospital = false" />
        </van-popup>
        <van-popup v-model:show="showStartTime" position="bottom" :style="{ height: '30%' }">
            <van-date-picker title="选择日期" :min-date="minDate" @confirm="showTimeConfirm"
                @cancel="showStartTime = false" />
        </van-popup>
        <van-popup v-model:show="showCompanion" position="bottom" :style="{ height: '30%' }">
            <van-picker :columns="showCompanionColumns" @confirm="showCompanionConfirm"
                @cancel="showCompanion = false" />
        </van-popup>
        <!-- 支付二维码弹窗 -->
        <van-dialog v-model:show="showCode" :show-confirm-button="false">
            <van-icon name="cross" class="close" @click="closeCode"></van-icon>
            <div>微信支付</div>
            <van-image width="150" height="150" :src="codeImg"></van-image>
            <div>请使用本人微信扫描二维码</div>
        </van-dialog>

    </div>
</template>

<script setup>
import { onMounted, getCurrentInstance, reactive, ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import statusBar from '../../components/statusBar.vue';
import Qrcode from 'qrcode';

// 初始化数据
const createInfo = reactive({
    companion: [],
    hospitals: [],
    service: {},
});

const { proxy } = getCurrentInstance();
onMounted(async () => {
    const { data } = await proxy.$api.h5Companion()
    // console.log(data);
    Object.assign(createInfo, data.data);   // 合并数据
    if (createInfo.companion.length == 0) {
        createInfo.companion = [
            {
                name: "小王",
                id: 1,
            },
            {
                name: "小李",
                id: 2,
            },
            {
                name: "小刘",
                id: 3,
            },
            {
                name: "小贺",
                id: 4,
            }
        ];
    };
    // console.log(createInfo);
});

// 返回按钮
const router = useRouter();
const goBack = () => {
    router.go(-1);
};
// 表单数据
const form = reactive({});

// 就诊医院
const showHospital = ref(false);
const showHospitalColumns = computed(() => {
    return createInfo.hospitals.map(item => {
        return { text: item.name, value: item.id };
    });
});
// 选择医院
const showHospConfirm = (item) => {
    form.hospital_id = item.selectedOptions[0].value;
    form.hospital_name = item.selectedOptions[0].text;
    showHospital.value = false;  // 关闭弹出层
};
// 选择就诊时间
const showStartTime = ref(false);
const currentDate = ref();
const minDate = ref(new Date());
const showTimeConfirm = (item) => {
    // 获取选择的时间，转化成时间戳
    const dateStr = item.selectedValues.join('-');
    currentDate.value = dateStr;
    form.starttime = new Date(dateStr).getTime();
    showStartTime.value = false;
};
// 选择陪诊师
const showCompanion = ref(false);
const showCompanionColumns = computed(() => {
    return createInfo.companion.map(item => {
        return { text: item.name, value: item.id };
    });
});
const companionName = ref();
const showCompanionConfirm = (item) => {
    form.companion_id = item.selectedOptions[0].value;
    companionName.value = item.selectedOptions[0].text;
    showCompanion.value = false;
};
// 提交订单
const submit = async () => {
    const params = [
        'hospital_id',
        'hospital_name',
        'demand',
        'companion_id',
        'receiveAdress',
        'tel',
        'starttime',
    ];
    for (const i of params) {
        if (!form[i]) {
            showNotify({ message: '请填写好订单的每一项' });
            return;
        };
    };
    const { data: orderRes } = await proxy.$api.createOrder(form);
    // console.log(orderRes);
    Qrcode.toDataURL(orderRes.data.wx_code).then((url) => {
        showCode.value = true;
        codeImg.value = url;
    });
}
// 支付弹窗
const showCode = ref(false);
const codeImg = ref();
const closeCode = () => {
    showCode.value = false;
    router.push('/order');
}
</script>

<style lang="less" scoped>
.container {
    background-color: #f0f0f0;
    height: 100vh;
}

.header {
    padding: 10px 0;
    text-align: center;
    line-height: 30px;
    font-size: 15px;

    .header-left {
        float: left;
    }
}

.cell {
    width: 95%;
    margin: 5px auto;
    background-color: #fff;

    ::v-deep(.van-field__control) {
        color: var(--van-cell-value-color);
    }

    ::v-deep(.van-cell__title) {
        display: flex;
        align-items: center;
    }

    .server-name {
        margin-left: 10px;
    }
}

.service-text {
    background: url() no-repeat center center;
    background-size: 20px;
}

.sumbit {
    position: absolute;
    bottom: 0;
}

::v-deep(.van-dialog__content) {
    text-align: center;
    padding: 20px;

    .close {
        position: absolute;
        left: 20px;
    }
}
</style>